// Always also include CF_bodyjs.js, so that functions like validateData will work.

function CF_ajtable(){

	this.syncVal = function(dbVariable,dbKey,elementID){
		var valElement = document.getElementById(elementID)
		if(valElement.type == 'checkbox'){
			if(valElement.checked){
				var newvalue = '1';
			}
			else{
				var newvalue = '0';
			}
		}
		else{
			var newvalue = valElement.value;
		}

		var oldValElID = 'OV_' + elementID;
		var oldvalue = document.getElementById(oldValElID).value;		
		
		
		
	
	//alert("dbVariable = " + dbVariable + " dbKey = " + dbKey + " elementID = " + elementID + " actionurl is " + 		this.url_syncval + " newvalue is " + newvalue + " oldvalue is " + oldvalue );
	if(oldvalue != newvalue){	
		// Do Validation
		if(typeof(this.arValRegEx) != 'undefined'){
		if(typeof(this.arValRegEx[dbVariable]) != 'undefined'){
			var isValid;
			isValid = this.validateData(dbVariable,dbKey,elementID);
			if(!isValid){
				return;
			}
		}
		}
	
	
		//$.getJSON(this.url_syncval,{sel: this.tablename, mode: "update", ks: this.keyvariables, kv: dbKey, field: dbVariable, value: newvalue},   function(data){
		$.ajax({
		  url: this.url_syncval,
		  dataType: 'json',
		  data: {sel: this.tablename, mode: "update", ks: this.keyvariables, kv: dbKey, field: dbVariable, value: newvalue},
		  timeout: 2000,
		  success: function(data){
		//alert(toSource(data));
		if(data != null){
		 if(data.status == "ok"){
				//alert("data is:" + data.status);
	     		//editCommand.execute();
				document.getElementById(oldValElID).value = newvalue;
	     }else{
				//alert("error2");
	     		//alert("Data Sync Error: " + data.msg);
	     		//document.getElementById(oldValElID).value = oldvalue;
				alert("Data Sync Error:  Data not updated");
				revert_value(data,elementID,oldvalue);
	     }
		 }else{
			alert("Data Sync Error:  Data not updated");
			revert_value(data,elementID,oldvalue);
		 }
	   },
	   error: function(jqXHR, textStatus, errorThrown){
			alert("Error: " + textStatus + " " + errorThrown);
			revert_value(data,elementID,oldvalue);
			//alert("Data Sync Error: " + data.msg);
	   }
	   
	   
	   
	   });
	} // end if oldvalue != newvalue
	}
	
	function revert_value(data,elementID,oldvalue){
		//alert("Data Sync Error: " + data.msg + "return to " + oldvalue);
		var valElement = document.getElementById(elementID)
		if(valElement.type == 'checkbox'){
			if(oldvalue == 0){
				valElement.checked = false;
			}
			else{
				valElement.checked = true;
			}
		}else{
			document.getElementById(elementID).value = oldvalue;	
		}
	}
	
	this.validateData = function(dbVariable,dbKey,elementID){
		var result = true;
		var pattern = this.arValRegEx[dbVariable];
		if(pattern != undefined){
			var theRegExp = new RegExp(pattern,"i");
			var theValue = document.getElementById(elementID).value;
			if(theRegExp.test(theValue)){
				result = true;
			}else{
				var message = this.arValMsg[dbVariable];
				alert(message);
				result = false;
				// Comment out to disable return to previouse value
				var oldValElID = 'OV_' + elementID;
				var oldvalue = document.getElementById(oldValElID).value;
				if(oldvalue != undefined){
					document.getElementById(elementID).value = oldvalue;
				}
			}
		
		}else{
			result = true;
		}
		return result;
	}
	
	
} // end CF_ajtable
